
package com.tools.cleanmaster.store;

import android.app.AlarmManager;
import android.text.TextUtils;

import com.tools.cleanmaster.utils.FeatureConfig;

import org.json.JSONException;
import org.json.JSONObject;

final class RecommendConfigs {

    private static final String JSON_KEY_RECOMMEND_SWITCH = "recommend_switch"; // 屏保推荐功能开关
    private static final String JSON_KEY_RECOMMEND_NEW_PROTECT_HOURS = "new_protect"; // 新用户保护时间，单位小时
    private static final String JSON_KEY_RECOMMEND_SHOW_TIMES = "show_times"; // 屏保推荐功能展示次数
    private static final String JSON_KEY_RECOMMEND_INTER_HOURS = "show_gap"; // 屏保推荐功能间隔展示时间，单位小时
    private static final String JSON_KEY_DEFAULT_SWITCH = "default_switch";//屏保默认开关

    static final boolean DEFAULT_RECOMMEND_SWITCH = false;
    static final long DEFAULE_RECOMMEND_NEW_PROTECT_TIME = 3 * AlarmManager.INTERVAL_HOUR;
    static final long DEFAULE_RECOMMEND_INTER_TIME = 72 * AlarmManager.INTERVAL_HOUR;
    static final int DEFAULT_RECOMMEND_SHOW_TIMES = 3;

    boolean recommendSwitch = DEFAULT_RECOMMEND_SWITCH;
    int recommendShowTimes = DEFAULT_RECOMMEND_SHOW_TIMES;
    long recommendNewProTime = DEFAULE_RECOMMEND_NEW_PROTECT_TIME;
    long recommendIntTime = DEFAULE_RECOMMEND_INTER_TIME;

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(", recommendSwitch :").append(recommendSwitch);
        builder.append(", recommendShowTimes: ").append(recommendShowTimes);
        builder.append(", recommendNewProTime: ").append(recommendNewProTime);
        builder.append(", recommendGapTime: ").append(recommendIntTime);
        return builder.toString();
    }

    static RecommendConfigs fromJson(String json) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }

        // 在debug模式下如果JSON格式存在问题，抛出异常用于发现错误
        // 在非debug模式下如果JSON格式存在问题，回落到默认值
        try {
            return fromJsonImpl(new JSONObject(json));
        } catch (JSONException e) {
            if (FeatureConfig.DEBUG_LOG) {
                throw new RuntimeException(e);
            } else {
                return new RecommendConfigs();
            }
        } catch (Throwable e) {
            if (FeatureConfig.DEBUG_LOG) {
                throw new RuntimeException(e);
            } else {
                return null;
            }
        }
    }

    private static RecommendConfigs fromJsonImpl(JSONObject jo) throws JSONException {
        boolean reSwitch = jo.optBoolean(JSON_KEY_RECOMMEND_SWITCH, DEFAULT_RECOMMEND_SWITCH);
        long reNewProTime = RecommendConfigs.DEFAULE_RECOMMEND_NEW_PROTECT_TIME;
        if (jo.has(JSON_KEY_RECOMMEND_NEW_PROTECT_HOURS)) {
            reNewProTime = jo.getInt(JSON_KEY_RECOMMEND_NEW_PROTECT_HOURS)
                    * AlarmManager.INTERVAL_HOUR;
        }
        long reIntTime = RecommendConfigs.DEFAULE_RECOMMEND_INTER_TIME;
        if (jo.has(JSON_KEY_RECOMMEND_INTER_HOURS)) {
            reIntTime = jo.getInt(JSON_KEY_RECOMMEND_INTER_HOURS) * AlarmManager.INTERVAL_HOUR;
        }
        int reShowTimes = jo.optInt(JSON_KEY_RECOMMEND_SHOW_TIMES, DEFAULT_RECOMMEND_SHOW_TIMES);

        RecommendConfigs configs = new RecommendConfigs();
        configs.recommendSwitch = reSwitch;
        configs.recommendNewProTime = reNewProTime;
        configs.recommendIntTime = reIntTime;
        configs.recommendShowTimes = reShowTimes;
        return configs;
    }
}
